//
//  UIView+CAIIBInspectable.m
//  Runner
//
//  Created by simon on 2021/5/10.
//

#import "UIView+CAIIBInspectable.h"
#import <objc/runtime.h>

@implementation UIView (CAIIBInspectable)


static char kAssociatedObjectKey_borderWidth;
- (void)setCaiBorderWidth:(CGFloat)caiBorderWidth {
    if (caiBorderWidth<0) {
        return;
    }
    objc_setAssociatedObject(self, &kAssociatedObjectKey_borderWidth, @(caiBorderWidth), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    self.layer.borderWidth = caiBorderWidth;
    [self setNeedsLayout];
}

- (CGFloat)caiBorderWidth {
    return [((NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_borderWidth)) floatValue];
}

static char kAssociatedObjectKey_borderColor;
- (void)setCaiBorderColor:(UIColor *)caiBorderColor {
    objc_setAssociatedObject(self, &kAssociatedObjectKey_borderColor, caiBorderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.layer.borderColor = caiBorderColor.CGColor;
    [self setNeedsLayout];
}

- (UIColor *)caiBorderColor {
    return (UIColor *)objc_getAssociatedObject(self, &kAssociatedObjectKey_borderColor);
}


static char kAssociatedObjectKey_cornerRadius;
- (void)setCaiCornerRadius:(CGFloat)caiCornerRadius {
    if (caiCornerRadius<0) {
        return;
    }
    objc_setAssociatedObject(self, &kAssociatedObjectKey_cornerRadius, @(caiCornerRadius), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    self.layer.cornerRadius = caiCornerRadius;
    self.layer.masksToBounds = caiCornerRadius > 0;
    [self setNeedsLayout];
}

- (CGFloat)caiCornerRadius {
    return [((NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_cornerRadius)) floatValue];
}


@end
